'use strict';

const Controller = require('egg').Controller;

class MainController extends Controller {
  async index() {
    // 首页的文章列表数据
    this.ctx.body = 'hi api';
  }
  // 判断用户名密码是否正确
  async checkLogin() {
    const userName = this.ctx.request.body.userName;
    const password = this.ctx.request.body.password;
    const sql = `SELECT user_name FROM admin_user WHERE user_name = '${userName}'  AND password = '${password}'`;
    const res = await this.app.mysql.query(sql);
    if (res.length > 0) {
      // 登录成功,进行session缓存
      const openId = new Date().getTime();
      this.ctx.session.openId = {
        openId,
      };
      this.ctx.body = {
        status: true,
        data: {
          openId,
        },
        message: "登录成功",
      };
    } else {
      this.ctx.body = {
        status: false,
        data: {},
        message: "用户名密码错误",
      };
    }
  }
  // 后台文章分类信息
  async getTypeInfo() {
    const resType = await this.app.mysql.select('type');
    this.ctx.body = {
      status: true,
      data: resType,
    };
  }
  // 添加文章
  async addArticle() {
    const tmpArticle = this.ctx.request.body;
    const result = await this.app.mysql.insert('article', tmpArticle);
    const insertSuccess = result.affectedRows === 1;
    const insertId = result.insertId;

    this.ctx.body = {
      isScuccess: insertSuccess,
      insertId,
    };
  }
  // 修改文章
  async updateArticle() {
    const tmpArticle = this.ctx.request.body;
    const result = await this.app.mysql.update('article', tmpArticle);
    const updateSuccess = result.affectedRows === 1;
    this.ctx.body = {
      isScuccess: updateSuccess,
    };
  }
  // 删除文章
  async delArticle() {
    const id = this.ctx.params.id;
    const res = await this.app.mysql.delete('article', { id });
    this.ctx.body = { data: res };
  }
  // 根据文章ID得到文章详情，用于修改文章
  async getArticleById() {
    const id = this.ctx.params.id;
    const sql = `SELECT article.id as id,
      article.title as title,
      article.introduce as introduce,
      article.article_content as article_content,
      FROM_UNIXTIME(article.created_at,'%Y-%m-%d' ) as created_at,
      article.view_count as view_count ,
      type.type_name as type_name ,
      type.id as type_id
      FROM article LEFT JOIN type ON article.type_id = type.id
      WHERE article.id=${id}`;
    const result = await this.app.mysql.query(sql);
    this.ctx.body = { data: result };
  }
  // 获得文章列表
  async getArticleList() {
    const sql = `SELECT article.id as id,
      article.title as title,
      article.introduce as introduce,
      FROM_UNIXTIME(article.created_at, '%Y-%m-%d') as created_at,
      article.view_count as view_count,
      type.type_name as type_name  FROM article LEFT JOIN type ON article.type_id = type.id ORDER BY article.id DESC`;
    const resList = await this.app.mysql.query(sql);
    this.ctx.body = { list: resList };
  }
}

module.exports = MainController;
